// 神秘学香薰蜡烛制作助手 - 数据模块

// 从JSON文件加载的知识库数据
const MYSTICAL_DATA = {
    "essential_oils": [
        {
            "id": "lavender",
            "name": "薰衣草",
            "english_name": "Lavender",
            "properties": ["平静", "保护", "治疗"],
            "effects": ["缓解焦虑", "促进睡眠", "净化能量场"],
            "price_per_10ml": 45,
            "energy_type": "平静型",
            "planet_association": ["月亮"],
            "moon_phase_suitable": ["新月", "满月"],
            "applications": ["睡眠香薰", "冥想香薰", "净化空间"],
            "keywords": ["焦虑", "失眠", "净化", "平静", "保护"],
            "icon": "fas fa-leaf"
        },
        {
            "id": "frankincense",
            "name": "乳香",
            "english_name": "Frankincense",
            "properties": ["灵性提升", "冥想", "净化"],
            "effects": ["连接更高意识", "深度冥想", "能量净化"],
            "price_per_10ml": 75,
            "energy_type": "灵性型",
            "planet_association": ["土星"],
            "moon_phase_suitable": ["满月", "新月"],
            "applications": ["宗教仪式", "冥想练习", "灵性成长"],
            "keywords": ["冥想", "灵性", "宗教", "净化", "意识提升"],
            "icon": "fas fa-fire"
        },
        {
            "id": "rosemary",
            "name": "迷迭香",
            "english_name": "Rosemary",
            "properties": ["记忆", "净化", "保护"],
            "effects": ["提升专注力", "增强记忆", "驱散负能量"],
            "price_per_10ml": 37.5,
            "energy_type": "智慧型",
            "planet_association": ["水星"],
            "moon_phase_suitable": ["上弦月"],
            "applications": ["学习香薰", "工作专注", "保护香薰"],
            "keywords": ["记忆", "专注", "学习", "保护", "净化"],
            "icon": "fas fa-brain"
        },
        {
            "id": "sandalwood",
            "name": "檀香",
            "english_name": "Sandalwood",
            "properties": ["神圣", "平静", "吸引"],
            "effects": ["冥想支持", "吸引正面能量", "灵性成长"],
            "price_per_10ml": 90,
            "energy_type": "神圣型",
            "planet_association": ["木星"],
            "moon_phase_suitable": ["新月", "满月"],
            "applications": ["冥想香薰", "灵性修行", "正能量吸引"],
            "keywords": ["冥想", "神圣", "正能量", "灵性", "平静"],
            "icon": "fas fa-om"
        },
        {
            "id": "rose",
            "name": "玫瑰",
            "english_name": "Rose",
            "properties": ["爱", "情感疗愈", "和谐"],
            "effects": ["吸引爱情", "修复情感创伤", "促进和谐"],
            "price_per_10ml": 115,
            "energy_type": "情感型",
            "planet_association": ["金星"],
            "moon_phase_suitable": ["上弦月", "满月"],
            "applications": ["爱情香薰", "情感疗愈", "关系和谐"],
            "keywords": ["爱情", "情感", "疗愈", "和谐", "吸引"],
            "icon": "fas fa-heart"
        },
        {
            "id": "eucalyptus",
            "name": "尤加利",
            "english_name": "Eucalyptus",
            "properties": ["净化", "清晰", "治疗"],
            "effects": ["清除负能量", "促进呼吸道健康", "提升清晰度"],
            "price_per_10ml": 30,
            "energy_type": "净化型",
            "planet_association": ["水星"],
            "moon_phase_suitable": ["下弦月", "满月"],
            "applications": ["空间净化", "健康香薰", "清晰思维"],
            "keywords": ["净化", "清晰", "健康", "负能量清除"],
            "icon": "fas fa-wind"
        }
    ],
    "wax_types": [
        {
            "id": "soy_wax",
            "name": "大豆蜡",
            "english_name": "Soy Wax",
            "properties": ["天然", "环保", "燃烧时间长"],
            "price_per_kg": 35,
            "burn_time_per_100g": "20-25小时",
            "suitable_for": ["日常使用", "长时间冥想", "环保选择"],
            "icon": "fas fa-seedling"
        },
        {
            "id": "beeswax",
            "name": "蜂蜡",
            "english_name": "Beeswax",
            "properties": ["天然", "净化空气", "神圣能量"],
            "price_per_kg": 60,
            "burn_time_per_100g": "25-30小时",
            "suitable_for": ["宗教仪式", "净化空间", "高端制作"],
            "icon": "fas fa-honey-pot"
        },
        {
            "id": "coconut_wax",
            "name": "椰子蜡",
            "english_name": "Coconut Wax",
            "properties": ["纯净", "香味扩散好", "燃烧干净"],
            "price_per_kg": 45,
            "burn_time_per_100g": "18-22小时",
            "suitable_for": ["香薰扩散", "小空间使用", "精油载体"],
            "icon": "fas fa-coconut"
        }
    ],
    "containers": [
        {
            "id": "ceramic_white",
            "name": "白色陶瓷罐",
            "material": "陶瓷",
            "color": "白色",
            "symbolism": ["纯净", "和平", "新开始"],
            "price": 15,
            "capacity": "200ml",
            "suitable_for": ["净化", "平衡", "开启新阶段"],
            "planet_association": ["月亮"],
            "chakra_connection": "顶轮",
            "icon": "fas fa-circle"
        },
        {
            "id": "ceramic_purple",
            "name": "紫色陶瓷罐",
            "material": "陶瓷",
            "color": "紫色",
            "symbolism": ["灵性", "智慧", "直觉"],
            "price": 18,
            "capacity": "200ml",
            "suitable_for": ["冥想", "占卜", "连接更高意识"],
            "planet_association": ["木星", "海王星"],
            "chakra_connection": "顶轮",
            "icon": "fas fa-circle"
        },
        {
            "id": "ceramic_green",
            "name": "绿色陶瓷罐",
            "material": "陶瓷",
            "color": "绿色",
            "symbolism": ["治疗", "丰盛", "自然"],
            "price": 16,
            "capacity": "200ml",
            "suitable_for": ["健康恢复", "财富吸引", "与自然连接"],
            "planet_association": ["金星", "地球"],
            "chakra_connection": "心轮",
            "icon": "fas fa-circle"
        },
        {
            "id": "ceramic_blue",
            "name": "蓝色陶瓷罐",
            "material": "陶瓷",
            "color": "蓝色",
            "symbolism": ["平静", "沟通", "保护"],
            "price": 17,
            "capacity": "200ml",
            "suitable_for": ["缓解压力", "促进表达", "创造安全感"],
            "planet_association": ["水星", "海王星"],
            "chakra_connection": "喉轮",
            "icon": "fas fa-circle"
        },
        {
            "id": "ceramic_red",
            "name": "红色陶瓷罐",
            "material": "陶瓷",
            "color": "红色",
            "symbolism": ["激情", "力量", "行动"],
            "price": 19,
            "capacity": "200ml",
            "suitable_for": ["爱情", "勇气", "激发能量"],
            "planet_association": ["火星"],
            "chakra_connection": "根轮",
            "icon": "fas fa-circle"
        },
        {
            "id": "ceramic_black",
            "name": "黑色陶瓷罐",
            "material": "陶瓷",
            "color": "黑色",
            "symbolism": ["保护", "驱散", "吸收"],
            "price": 20,
            "capacity": "200ml",
            "suitable_for": ["去除负能量", "设定界限", "深层净化"],
            "planet_association": ["土星"],
            "chakra_connection": "根轮",
            "icon": "fas fa-circle"
        }
    ],
    "packaging": [
        {
            "id": "simple_box",
            "name": "简约包装盒",
            "description": "环保纸质包装盒",
            "price": 5,
            "suitable_for": ["日常使用", "环保选择"],
            "icon": "fas fa-box"
        },
        {
            "id": "luxury_box",
            "name": "豪华包装盒",
            "description": "精美礼品包装盒，含丝带装饰",
            "price": 12,
            "suitable_for": ["礼品赠送", "特殊场合"],
            "icon": "fas fa-gift"
        },
        {
            "id": "mystical_box",
            "name": "神秘学包装盒",
            "description": "带有神秘学符号的特制包装盒",
            "price": 15,
            "suitable_for": ["仪式使用", "收藏纪念"],
            "icon": "fas fa-magic"
        }
    ],
    "effects": [
        {
            "id": "sleep",
            "name": "改善睡眠",
            "description": "帮助放松身心，促进深度睡眠",
            "keywords": ["失眠", "睡眠", "安眠", "休息"],
            "recommended_oils": ["lavender"],
            "optimal_moon_phase": "新月",
            "suitable_planets": ["月亮"],
            "icon": "fas fa-moon"
        },
        {
            "id": "focus",
            "name": "学习专注",
            "description": "提升专注力和记忆力，适合学习工作",
            "keywords": ["学习", "专注", "记忆", "考试"],
            "recommended_oils": ["rosemary"],
            "optimal_moon_phase": "上弦月",
            "suitable_planets": ["水星"],
            "icon": "fas fa-brain"
        },
        {
            "id": "love",
            "name": "爱情吸引",
            "description": "增强个人魅力，吸引爱情能量",
            "keywords": ["爱情", "感情", "吸引", "恋爱"],
            "recommended_oils": ["rose"],
            "optimal_moon_phase": "上弦月",
            "suitable_planets": ["金星"],
            "icon": "fas fa-heart"
        },
        {
            "id": "wealth",
            "name": "财富增长",
            "description": "吸引丰盛能量，促进事业发展",
            "keywords": ["财富", "金钱", "丰盛", "事业"],
            "recommended_oils": ["sandalwood"],
            "optimal_moon_phase": "上弦月",
            "suitable_planets": ["木星"],
            "icon": "fas fa-coins"
        },
        {
            "id": "protection",
            "name": "保护驱邪",
            "description": "建立能量防护，驱散负面能量",
            "keywords": ["保护", "驱邪", "负能量", "防护"],
            "recommended_oils": ["frankincense", "rosemary"],
            "optimal_moon_phase": "满月",
            "suitable_planets": ["土星", "火星"],
            "icon": "fas fa-shield-alt"
        },
        {
            "id": "purification",
            "name": "净化空间",
            "description": "清除空间负能量，提升环境振动",
            "keywords": ["净化", "清洁", "清理", "能量清除"],
            "recommended_oils": ["eucalyptus", "lavender"],
            "optimal_moon_phase": "满月",
            "suitable_planets": ["月亮"],
            "icon": "fas fa-wind"
        }
    ]
};

// 应用状态管理
const AppState = {
    currentStep: 1,
    maxSteps: 6,
    selectedEffect: null,
    selectedOils: [],
    selectedWax: null,
    waxAmount: 200,
    oilConcentration: 6, // 默认6%
    selectedContainer: null,
    selectedPackaging: null,
    totalCost: 0,
    sellingPrice: 0,
    profitMargin: 0,
    
    // 重置状态
    reset() {
        this.currentStep = 1;
        this.selectedEffect = null;
        this.selectedOils = [];
        this.selectedWax = null;
        this.waxAmount = 200;
        this.oilConcentration = 6;
        this.selectedContainer = null;
        this.selectedPackaging = null;
        this.totalCost = 0;
        this.sellingPrice = 0;
        this.profitMargin = 0;
    },
    
    // 计算成本
    calculateCost() {
        let cost = 0;
        
        // 蜡的成本
        if (this.selectedWax) {
            const waxCost = (this.waxAmount / 1000) * this.selectedWax.price_per_kg;
            cost += waxCost;
        }
        
        // 精油成本
        this.selectedOils.forEach(oil => {
            const oilAmount = (this.waxAmount * this.oilConcentration / 100) / this.selectedOils.length;
            const oilCost = (oilAmount / 10) * oil.price_per_10ml;
            cost += oilCost;
        });
        
        // 容器成本
        if (this.selectedContainer) {
            cost += this.selectedContainer.price;
        }
        
        // 包装成本
        if (this.selectedPackaging) {
            cost += this.selectedPackaging.price;
        }
        
        this.totalCost = Math.round(cost * 100) / 100;
        return this.totalCost;
    },
    
    // 更新利润率
    updateProfitMargin() {
        if (this.totalCost > 0 && this.sellingPrice > 0) {
            this.profitMargin = calculateProfitMargin(this.totalCost, this.sellingPrice);
        } else {
            this.profitMargin = 0;
        }
    },
    
    // 获取产品数据
    getProductData() {
        return {
            id: generateId(),
            name: this.generateProductName(),
            effect: this.selectedEffect,
            oils: this.selectedOils,
            wax: this.selectedWax,
            waxAmount: this.waxAmount,
            oilConcentration: this.oilConcentration,
            container: this.selectedContainer,
            packaging: this.selectedPackaging,
            cost: this.totalCost,
            sellingPrice: this.sellingPrice,
            profitMargin: this.profitMargin,
            createdAt: new Date().toISOString(),
            status: 'in_stock'
        };
    },
    
    // 生成产品名称
    generateProductName() {
        if (!this.selectedEffect) return '神秘学香薰蜡烛';
        
        const effectName = this.selectedEffect.name;
        const oilNames = this.selectedOils.map(oil => oil.name).join('&');
        return `${effectName} - ${oilNames}香薰蜡烛`;
    }
};

// 智能推荐系统
const RecommendationEngine = {
    // 根据功效推荐精油
    recommendOilsByEffect(effectId, effects = null, oils = null) {
        const effectsData = effects || MYSTICAL_DATA.effects;
        const oilsData = oils || MYSTICAL_DATA.essential_oils;
        
        const effect = effectsData.find(e => e.id === effectId);
        if (!effect) return [];
        
        return effect.recommended_oils.map(oilId => 
            oilsData.find(oil => oil.id === oilId)
        ).filter(oil => oil);
    },
    
    // 根据精油推荐容器颜色
    recommendContainerByOils(oils, containers = null) {
        const containersData = containers || MYSTICAL_DATA.containers;
        
        if (!oils || oils.length === 0) return containersData;
        
        // 获取精油的行星关联
        const planets = [...new Set(oils.flatMap(oil => oil.planet_association))];
        
        // 根据行星推荐容器
        const recommendedContainers = containersData.filter(container => 
            container.planet_association.some(planet => planets.includes(planet))
        );
        
        return recommendedContainers.length > 0 ? recommendedContainers : containersData;
    },
    
    // 根据关键词搜索功效
    searchEffectsByKeywords(keywords, effects = null) {
        const effectsData = effects || MYSTICAL_DATA.effects;
        const searchTerms = keywords.toLowerCase().split(' ');
        
        return effectsData.filter(effect => 
            searchTerms.some(term => 
                effect.keywords.some(keyword => keyword.includes(term)) ||
                effect.name.toLowerCase().includes(term) ||
                effect.description.toLowerCase().includes(term)
            )
        );
    },
    
    // 获取建议售价
    getSuggestedPrice(cost) {
        const baseMarkup = 2.5; // 基础加价倍数
        const premiumMarkup = 3.0; // 高端加价倍数
        
        return {
            minimum: Math.ceil(cost * baseMarkup),
            suggested: Math.ceil(cost * (baseMarkup + 0.3)),
            premium: Math.ceil(cost * premiumMarkup)
        };
    }
};

// 导出数据和状态
window.MYSTICAL_DATA = MYSTICAL_DATA;
window.AppState = AppState;
window.RecommendationEngine = RecommendationEngine;

// 数据验证函数
function validateProductData(data) {
    const required = ['effect', 'oils', 'wax', 'container', 'packaging'];
    return required.every(field => data[field] && 
        (Array.isArray(data[field]) ? data[field].length > 0 : true)
    );
}

window.validateProductData = validateProductData;